import { Injectable,Logger } from '@nestjs/common';
import axios from 'axios';
import * as cheerio from 'cheerio';
import * as fs from 'fs';
import  xlsx from 'node-xlsx';


@Injectable()
export class AppService {
  private readonly logger=new Logger(AppService.name);
  
  constructor(){
    this.init();
  }


  

  async init(){
    const url = `http://quotes.money.163.com/f10/zycwzb_600519.html#01c01`;
    const res = await axios.get(url);
    const $ = cheerio.load(res.data);
    const that=this
    const titlearr=[];
    const contarr=[]
    const   title =$('.table_bg001.border_box.limit_sale.scr_table tr').eq(0).find('th').each(function(i,ele){
      
      titlearr.push($(this).text());
      // that.logger.log($(this).text());
    });
    const cont =$('.table_bg001.border_box.limit_sale.scr_table tr').eq(11).find('td').each(function(i,ele){
      
      contarr.push($(this).text());
      // that.logger.log($(this).text());
    });
    const obj=[
      {
        name:'mt',
        data:[
          [...titlearr],
          [...contarr]
        ]
      }
    ];
    this.logger.log(JSON.stringify(obj));

    fs.writeFileSync('./mt.xlsx',xlsx.build(obj),'binary');
  
   

    // this.logger.log(title);
    // table_bg001
    
  }

   getHello(): string {

    

    return 'Hello World!';
  }
}
